package mymyoux.io.sockets.servers.mserverClasses
{
	import mymyoux.io.sockets.mapping.Telegramme;
	import mymyoux.io.sockets.servers.mserverClasses.SBinarySocket;
	import mymyoux.signals.MetaSignal;
	import mymyoux.signals.Signal;

	public class People
	{
		private var _id:uint;
		private var _socket:SBinarySocket;
		private var _signalDisconnect:Signal;
		private var _signalRoom:Signal;
		private var _signalLog:Signal;
		private var _signalRoomData:MetaSignal;
		private var _rooms:Vector.<String>;
		public function People()
		{
			_id = 0;
			_signalDisconnect = new Signal();
			_signalRoom = new Signal(String, Boolean);
			_signalLog = new Signal(uint);
			_signalRoomData = new MetaSignal(Telegramme);
			_rooms = new Vector.<String>();
		}
		public function addRoom(name:String):void
		{
			if(_rooms.indexOf(name) < 0)
			{
				_rooms.push(name);
			}
		}
		public function remRoom(name:String):void
		{
			var index:int = _rooms.indexOf(name);
			if(index > -1)
			{
				_rooms.splice(index, 1);
			}
		}
		private function destroy():void
		{
			_socket.signalData.remove(onData);
			_socket = null;
			_signalDisconnect.removeAll();
			_signalRoom.removeAll();
			_signalLog.removeAll();
			_signalRoomData.removeAll();
		}
		public function get rooms():Vector.<String>
		{
			return _rooms;
		}
		public function get signalDisconnect():Signal
		{
			return _signalDisconnect;
		}
		public function get signalLog():Signal
		{
			return _signalLog;
		}


		public function get signalRoom():Signal
		{
			return _signalRoom;
		}
		public function get signalRoomData():MetaSignal
		{
			return _signalRoomData;
		}
		public function get socket():SBinarySocket
		{
			return _socket;
		}

		public function set socket(value:SBinarySocket):void
		{
			_socket = value;
			_socket.signalData.add(onData);
			_socket.signalStatus.addOnce(onStatus);
		}

		public function get id():uint
		{
			return _id;
		}
		public function set id(value:uint):void
		{
			_id = value;	
		}
		public function get logged():Boolean
		{
			return _id > 0;
		}
		private function onStatus(connected:Boolean):void
		{
			if(connected)
			{
				_socket.signalStatus.addOnce(onStatus);
			}else
			{
				_signalDisconnect.dispatch();
				destroy();
			}
		}
		private function onData(telegramme:Telegramme):void
		{
			switch(telegramme.command)
			{
				case Telegramme.LOGIN:
					if(!logged)
					{
						_signalLog.dispatch(telegramme.id);
					}
					/*if(!people.logged)
					{
						telegramme = new Telegramme();
						telegramme.command = Telegramme.LOGIN;
						if(!peoples.contains(telegramme.id))
						{
							telegramme.action = Telegramme.LOGIN_OK;
							people.id = telegramme.id;
							peoples.add(people);
						}else
						{
							telegramme.action = Telegramme.LOGIN_ALREADY_CONNECTED;
						}
						socket.write(telegramme);
					}*/
					break;
				case Telegramme.GET_ROOM:
					_signalRoom.dispatch(telegramme.room, true);
					/*
					var response:Telegramme = new Telegramme();
					response.action = Telegramme.ROOM_OK;
					response.command = Telegramme.GET_ROOM;
					response.room = t.room;
					//	telegramme.object = response;
					//socket.write(telegramme);
					socket.write(response);*/
					break;
				case Telegramme.LEFT_ROOM:
					_signalRoom.dispatch(telegramme.room, false);
					break;
				case Telegramme.SEND_ROOM:
					_signalRoomData.dispatch(telegramme.room, telegramme);
					/*
					log("messageroom");
					socket.write(t);*/
					break;
				default:
					//TODO:default data
			}
		}
	}
}